<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############


if(!isset($_SESSION['install']['database'])) redirect('?step=database');

if(count($_POST)>0){
    $error = false;

    $domain = post('domain');
    $baseUrl = rtrim(str_replace('\\', '/', post('baseUrl')), '/');
    $publicDir = rtrim(str_replace('\\', '/', post('publicDir')), '/');
    $baseDir = rtrim(str_replace('\\', '/', post('baseDir')), '/');
    $secureUrl = rtrim(str_replace('\\', '/', post('secureUrl')), '/');
    $pathPrefix = rtrim(str_replace('\\', '/', post('prefix')));
    $adminFolder = rtrim(str_replace('\\', '/', post('adminFolder')));
    $adminUrl = $baseUrl.'/'.str_replace('//', '/', trim($pathPrefix.'/'.$adminFolder, '/'));

    if(!is_dir($baseDir)){
        $error = true;
        $errors[] = 'Non-public folder is not a directory';
    }

    if(!is_dir($publicDir)){
        $error = true;
        $errors[] = 'Public folder is not a directory';
    }

    if(!is_dir($baseDir.'/classes')){
        $error = true;
        $errors[] = $baseDir.'/classes does not exist, please make sure the path to non-public root folder is correct.';
    }elseif(!file_exists($baseDir.'/classes/JFX.php')){
        $error = true;
        $errors[] = $baseDir.'/classes/JFX.php does not exist, please make sure the path to non-public root folder is correct.';
    }

    if(!$error){
        $_SESSION['install']['paths'] = array(
            'baseUrl' => $baseUrl,
            'publicDir' => $publicDir,
            'baseDir' => $baseDir,
            'adminUrl' => $adminUrl,
            'secureUrl' => $secureUrl,
            'prefix' => $pathPrefix,
            'adminFolder' => $adminFolder,
            'domain' => $domain
        );

        redirect('?step=settings');
    }

}

// get our default values
$domain = $_SERVER['SERVER_NAME'];
$dirname = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__));
$dirname = explode('/', $dirname);
array_pop($dirname);
$baseDir = implode('/', $dirname);
$publicDir = str_replace(DIRECTORY_SEPARATOR, '/', rtrim(dirname(JFX_EXEC_FILE), DIRECTORY_SEPARATOR));

$requestUri = str_replace('index.php', '', $_SERVER['REQUEST_URI']);
$requestUri = str_replace('//', '/', $requestUri);
$requestUri = explode('?', $requestUri);
$requestUri = $requestUri[0];
$pathPrefix = '/'.trim($requestUri, '/');
$baseUrl = rtrim('http://'.$_SERVER['SERVER_NAME'].'/'.$request_uri, '/');
$secureUrl = rtrim('https://'.$_SERVER['SERVER_NAME'].'/'.$request_uri, '/');

$adminUrl = '/admin';

?>
<form method="post">
    <div class="form-field">
        <label>Domain Name</label>
        <input type="text" name="domain" class="wide" value="<?php echo post('domain', $domain) ?>" />
    </div>
    <div class="form-field">
        <label>Path to non-public root folder</label>
        <input type="text" name="baseDir" class="wide" value="<?php echo post('baseDir', $baseDir) ?>" />
    </div>
    <div class="form-field">
        <label>Path to public root folder</label>
        <input type="text" name="publicDir" class="wide" value="<?php echo post('publicDir', $publicDir) ?>" />
    </div>
    <!--div class="form-field">
        <label>Base URL</label>
        <input type="text" name="baseUrl" class="wide" value="<?php echo post('baseUrl', $baseUrl) ?>" />
    </div-->
    <div class="form-field">
        <label>Path Prefix</label>
        <input type="text" name="prefix" class="wide" value="<?php echo post('prefix', rtrim($pathPrefix, '/')) ?>" />
    </div>
    <!--div class="form-field">
        <label>Secure URL</label>
        <input type="text" name="secureUrl" class="wide" value="<?php echo post('secureUrl', $secureUrl) ?>" />
    </div-->
    <div class="form-field">
        <label>Admin Folder Name</label>
        <input type="text" name="adminFolder" class="wide" value="<?php echo post('adminFolder', $adminUrl) ?>" />
    </div>
    <div class="form-field">
        <label>&nbsp;</label>
        <input type="submit" name="submit" value="Proceed" />
    </div>

</form>




